
import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/constant/constant.dart';
import 'package:flutter_ncsjqz_getx/page/my/history_relese/history_release_view.dart';
import 'package:flutter_ncsjqz_getx/page/rong_im/chat_window/chat_view.dart';
import 'package:flutter_ncsjqz_getx/page/rong_im/commom/chat_history/chat_record_view.dart';
import 'package:flutter_ncsjqz_getx/util/get_user_util.dart';
import 'package:flutter_switch/flutter_switch.dart';
import 'package:get/get.dart';

import '../../util/rong_widget.dart';
import 'chat_setting_logic.dart';
import 'more_info_view.dart';

/// <summary>
/// author：zwb
/// dateTime： 2021/12/15 11:09
/// filePath： lib/page/rong_im/chat_setting_view.dart
/// desc: 单聊 对话设置
/// <summary>
///
class ChatSettingPage extends StatefulWidget {
  final String? ryUserId; // 融云用户id
  final int? friendId; // 好友id
  final int? type; // 1 对话设置 2 添加好友
  ChatSettingPage({this.ryUserId, this.friendId, this.type: 1});
  @override
  _ChatSettingPageState createState() => _ChatSettingPageState();
}
class _ChatSettingPageState extends State<ChatSettingPage> {
  final logic = Get.put(ChatSettingLogic());

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    logic.ryUserId = widget.ryUserId;
    logic.friendId = widget.friendId;
    logic.getFriendInfo();
  }
  @override
  Widget build(BuildContext context) {
    return GetBuilder<ChatSettingLogic>(
      builder: (controller) {
        String title = '';
        if(widget.type==1){
          title = "对话设置";
        }
        if(widget.type==2 && !logic.friendInfoData!.isFriend!){
          title = "添加好友";
        }
        if(Constant.userDetailData!.id == logic.queryUserDetaisData!.userId){
          title = "自己";
        }
        return Scaffold(
          appBar: RongWidget.appBarWidget(title: title),
          body:  ListView(
            padding: EdgeInsets.all(0),
            children: [
              headUserInfo(),

              Offstage(
                offstage: widget.type != 1 || Constant.userDetailData!.id == logic.queryUserDetaisData!.userId,
                child: RongWidget.rowBaseWidget(
                    padding: EdgeInsets.all(15),
                    leftText: "设置备注",
                    rightText: "${logic.queryUserDetaisData!.remark ?? ''}",
                    onTap: (){
                      Get.dialog(
                        Center(
                          child: Material(
                            color: Colors.transparent,
                            child: ClipRRect(
                              child: Container(
                                  width: Get.width * 0.6,
                                  height: 140,
                                  color: Colors.white,
                                  padding: EdgeInsets.all(15),
                                  child: Center(
                                    child: Column(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      crossAxisAlignment: CrossAxisAlignment.center,
                                      children: [
                                        RongWidget.title("设置备注"),
                                        RongWidget.rongInput(
                                          controller: logic.textEditingControllerName,
                                          circular: 5,
                                          hintText: "${'输入备注名称'}",
                                        ),
                                        RongWidget.gradientButton("设置",onTap: (){
                                          Get.back();
                                          logic.queryUserDetaisData!.remark = logic.textEditingControllerName.value.text;
                                          logic.setRemark(logic.queryUserDetaisData!.userId,Disturb: logic.disturb);
                                        }),
                                      ],
                                    ),
                                  )
                              ),
                              borderRadius: BorderRadius.circular(5),
                            ),
                          ),
                        ),
                      );
                    }
                ),
              ),

              RongWidget.rowBaseWidget(
                padding: EdgeInsets.all(15),
                leftText: "更多信息",
                onTap: ()=> Get.to(()=>MoreInfoPage(friendInfoData: logic.friendInfoData,)),
              ),

              RongWidget.rowBaseWidget(
                crossAxisAlignment: CrossAxisAlignment.start,
                padding: EdgeInsets.all(15),
                leftText: "历史发布",
                right: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      width: Get.width - 120,
                      height: 60,
                      child: ListView(
                        scrollDirection: Axis.horizontal,
                        children: [
                          ...List.generate(logic.friendInfoData!.postList == null || logic.friendInfoData!.postList!.length == 0 ? 0 : logic.friendInfoData!.postList!.length, (index){
                            String pic = '';
                            if(logic.friendInfoData!.postList![index] != null && logic.friendInfoData!.postList![index].postUrls!= null && logic.friendInfoData!.postList![index].postUrls!.length>0){
                              pic = logic.friendInfoData!.postList![index].postUrls![0];
                            }
                            return RongWidget.imgNet(
                              pic,
                              width: 60,
                              height: 60,
                              padding: EdgeInsets.only(right: 10),
                              borderRadius: BorderRadius.circular(5),
                            );
                          }),
                        ],
                      ),
                    ),
                    Icon(Icons.chevron_right,color:  Colors.grey.shade400,)
                  ],
                ),
                onTap: (){
                  print("去查看帖子列表");
                  Get.to(()=>HistoryReleasePage(myReleseDataList: logic.myReleseDataList,type: 2,));
                }
              ),

              // 屏蔽关注自己
              Offstage(
                offstage: Constant.userDetailData!.id == logic.queryUserDetaisData!.userId,
                child: RongWidget.rowBaseWidget(
                  padding: EdgeInsets.all(15),
                  leftText: "消息免打扰",
                  right: FlutterSwitch(
                    height: 25,
                    width: 50,
                    toggleSize: 22.0,
                    padding: 0,
                    value: logic.disturb,
                    activeColor: Colors.green,
                    onToggle: (val) => logic.setRemark(logic.queryUserDetaisData!.userId,Disturb: !logic.disturb),
                  ),
                ),
              ),

              // 屏蔽关注自己
              Offstage(
                offstage: Constant.userDetailData!.id == logic.queryUserDetaisData!.userId,
                child: RongWidget.rowBaseWidget(
                  padding: EdgeInsets.all(15),
                  leftText: "关注他",
                  right: FlutterSwitch(
                    height: 25,
                    width: 50,
                    toggleSize: 22.0,
                    padding: 0,
                    value: logic.isAttention,
                    activeColor: Colors.green,
                    onToggle: (val) => logic.setFollow(val),
                  ),
                ),
              ),

              Offstage(
                offstage:  widget.type == 2,
                child: RongWidget.rowBaseWidget(
                  padding: EdgeInsets.all(15),
                  leftText: "查找聊天内容",
                  onTap: ()=> Get.to(()=>ChatRecordPage(targetId: logic.ryUserId,conversationType: 1,)),
                ),
              ),

              Offstage(
                  offstage:  widget.type != 1,
                  child: InkWell(
                    child: Container(
                      color: Colors.white,
                      padding: EdgeInsets.all(15),
                      alignment: Alignment.center,
                      child: RongWidget.title("清空聊天记录",color: Colors.red,onTap: (){
                        logic.deleteMessages(logic.ryUserId,(v){
                          Get.back();
                          Get.back();
                        });
                      }),
                    ),
                  ),
              ),

              Offstage(
                // 添加好友才显示
                offstage: widget.type != 2,
                // 是好友才可私聊
                child: widget.type == 2 && !logic.friendInfoData!.isFriend! ? SizedBox():Container(
                  color: Colors.white,
                  margin: EdgeInsets.only(top: 1,),
                  child: MaterialButton(
                    onPressed: (){
                      Get.to(()=>ChatPage(
                        conversationType: 1,
                        targetId: logic.ryUserId!,
                        title:logic.friendInfoData!.userName,
                        userPic: logic.friendInfoData!.userPic,
                        friendId: logic.friendId,
                      ));
                    },
                    child: RongWidget.text("发起私聊"),
                  ),
                ),
              ),

              Offstage(
                // 添加好友才显示 并且屏蔽自己
                offstage: widget.type != 2 || getUserUtil().ryUserId == widget.ryUserId,
                // 不是好友 提示加好友
                child: widget.type == 2 && logic.friendInfoData!.isFriend! ? SizedBox():Container(
                  color: Colors.white,
                  margin: EdgeInsets.only(top: 1,),
                  child: MaterialButton(
                    onPressed: () async{
                      logic.friendApply();
                    },
                    child: RongWidget.text("添加好友",color: Colors.green,fonSize: 16),
                  ),
                ),
              ),

            ],
          ),
        );
      },
    );
  }

  @override
  void dispose() {
    Get.delete<ChatSettingLogic>();
    super.dispose();
  }

  Widget headUserInfo() {
    return Container(
      margin: EdgeInsets.only(top: 10,bottom: 5),
      padding: EdgeInsets.only(top: 15,left: 15),
      color: Colors.white,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          RongWidget.imgNet('${logic.friendInfoData!.userPic}',width: 65,height: 65,borderRadius: BorderRadius.circular(10)),
          SizedBox(width: 10,),
          Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              logic.queryUserDetaisData!.remark == null ||  logic.queryUserDetaisData!.remark == "" ? SizedBox(): Text('${logic.queryUserDetaisData!.remark}',style: TextStyle(
                  fontSize: 18,fontWeight: FontWeight.bold
              ),),
              Text('昵称:  ${logic.friendInfoData!.userName}',style: TextStyle(
                  fontSize: 14,
                  color: Colors.grey
              ),),
              Text('ID号:  ${logic.friendInfoData!.uniqueId}',style: TextStyle(
                  fontSize: 14,
                  color: Colors.grey
              ),),
              Text('地区:  ${logic.friendInfoData!.location ?? ''}',style: TextStyle(
                  fontSize: 14,
                  color: Colors.grey
              ),),
              SizedBox(height: 5,),
            ],
          )
        ],
      ),
    );
  }
}